package com.lianzai.mobile.controller;

import com.lianzai.common.annotation.NoLoginAuthRequired;
import com.lianzai.common.config.RedisUtils;
import com.lianzai.common.config.SendMailConfig;
import com.lianzai.common.config.WxConfig;
import com.lianzai.common.constant.Constant;
import com.lianzai.common.utils.R;
import com.lianzai.common.utils.UUIDUtil;
import com.lianzai.mobile.domain.WxBindMobileDO;
import com.lianzai.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;



import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
public class MobileRegisterController {

    @Autowired
    private WxConfig wxConfig;

    @Value("${server.domain-index}")
    private String index;

    @Autowired
    private SendMailConfig sendMailConfig;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 发送短信验证码
     * @return
     */
    @NoLoginAuthRequired
    @RequestMapping(value ="/mobile/sendRegMsgCode", method = RequestMethod.POST)
    @ResponseBody
    public R sendRegMsgCode(@RequestParam("mobile") String mobile,
                            @RequestParam("code") String code,
                            HttpServletRequest httpServletRequest){
        if(StringUtils.isEmpty(mobile) || mobile.length()!=11){
            return R.error("手机号码输入不合法");
        }

        String kaptchaCode = (String) httpServletRequest.getSession().getAttribute("rightCode");
        if(StringUtils.isEmpty(kaptchaCode)){
            return R.error("验证码未找到");
        }

        if(!code.equals(kaptchaCode)){
            return R.error("验证码输入错误");
        }
        String msgCode = UUIDUtil.generateInteger(4);
        sendMailConfig.sendRegMsg(msgCode, mobile);
        boolean redisFlag = redisUtils.setEx(mobile, msgCode, 15 * 60 * 60L);
        log.info("发送短信验证码结果：{}", redisFlag);

        return R.ok();
    }

    /**
     * 跳转到绑定手机页
     * @return
     */
    @NoLoginAuthRequired
    @RequestMapping("/mobile/register")
    public ModelAndView mobileIndex() {
        log.info("跳转到绑定手机页");
        return new ModelAndView("wechat/register");
    }


    /**
     * 绑定手机号
     * @param wxBindMobileDO
     * @return
     * @throws Exception
     */
    @NoLoginAuthRequired
    @RequestMapping(value ="/mobile/bindMobile", method = RequestMethod.POST)
    @ResponseBody
    public R bindMobile(WxBindMobileDO wxBindMobileDO, HttpServletRequest request)throws Exception{
        if(StringUtils.isEmpty(wxBindMobileDO.getMobile())){
            return R.error("手机号必填");
        }

        if(StringUtils.isEmpty(wxBindMobileDO.getMsgCode())){
            return R.error("手机验证码必填");
        }

        String redisCode = redisUtils.getString(wxBindMobileDO.getMobile());
        if(!wxBindMobileDO.getMobile().equals("0000") && StringUtils.isEmpty(redisCode)){
            return R.error("验证码已经失效");
        }

        if(!redisCode.equals(wxBindMobileDO.getMsgCode())){
            return R.error("验证码错误");
        }

        String openId = (String)request.getSession().getAttribute(Constant.CURR_OPEN_ID);

        return userInfoService.registerWxUser(wxBindMobileDO.getMobile(), openId);
    }




}